using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace EnemyStates
{
    public class Guarding : FSMBehaviour
    {
        Enemy m_Owner;
        bool m_IsPatroller = false;
        bool m_IsGuarding;
        List<Vector3>[] patrollingPaths;
        int patrolIndex;
        Vector3 returningPosition;
        Vector3 m_OriginalEyeDirection;
        bool m_ReachOriginalPos = false;
        public Guarding(Enemy owner, List<Transform> transforms)
        {
            this.m_Owner = owner;
            m_OriginalEyeDirection = owner.transform.forward;
            m_IsPatroller = SetPatrolPoints(transforms);
            if (m_IsPatroller == true)
            {
                returningPosition = transforms[0].position;
            }
            else
            {
                returningPosition = owner.transform.position;
                m_IsGuarding = true;
            }
        }

        public bool SetPatrolPoints(List<Transform> transforms)
        {
            if (transforms == null || transforms.Count == 0) return false;
            if (transforms.Count == 1)
            {
                Debug.LogError("There's less than 2 patrolling points");
                return false;
            }
            patrollingPaths = new List<Vector3>[transforms.Count];
            for (int i = 0; i < patrollingPaths.Length; i++)
            {
                //si estoy en el ultimo del for busco el camino para volver al primero
                if (i != patrollingPaths.Length - 1)
                {
                    patrollingPaths[i] = m_Owner.GetPath(transforms[i].position, transforms[i + 1].position);
                }
                else
                {
                    patrollingPaths[i] = m_Owner.GetPath(transforms[i].position, transforms[0].position);
                }
                if (patrollingPaths[i] == null)
                {
                    Debug.LogError("Patrol points can't connect with each other");
                    return false;
                }
            }
            return true;
        }

        public override void Update()
        {
            base.Update();

            m_Owner.RotateEye();
            m_Owner.UpdateEyeDirection();

            //si ve a una personaje del jugador
            if (m_Owner.IsCharacterInSight())
            {
                m_Owner.SendEvent(ENEMYEVENTS.SEEPLAYER);
                return;
                //Debug.Log("TE ESTA VIENDO");
            }
            //si no esta patrullando por algun motivo (por ej que viene de otro estado)
            if (m_IsGuarding == false)
            {

                m_Owner.MoveTo(returningPosition);
                m_IsGuarding = true;
            }
            else if(m_IsPatroller)
            {
                if (m_Owner.m_ReachDestination)
                {
                    SetNextPatrol();
                }
            }
            else if (m_Owner.m_ReachDestination && m_ReachOriginalPos == false)
            {
                m_Owner.transform.LookAt(m_Owner.transform.position + m_OriginalEyeDirection);
                m_ReachOriginalPos = true;
            }

            m_Owner.LookForEnemyCorpses();
        }
        void SetNextPatrol()
        {
            m_Owner.SetCurrentPath(patrollingPaths[patrolIndex]);
            patrolIndex++;
            if (patrolIndex >= patrollingPaths.Length)
            {
                patrolIndex = 0;
            }
        }

        public override void OnDisable()
        {
            m_IsGuarding = false;
            m_ReachOriginalPos = false;
        }
    }
}
